/**
 * Source file: ClaimantStatement_AutoLookups.ejs
 * Copyright (C) 2013 : Quantum Solutions
 * Id: $Id$
 * Date: $Date$
 */

//Turn on your selected Environment before exporting your form from TeleForm.
// serverName is also defined in ConnectWebService (should be the same value)
//var serverName = "http://127.0.0.1:50057"; // rob VS
//var serverName = "http://WIN-OV9FIKDJ4BV:88"; // Willie test
//var serverName = "http://suglifeabiqa01:88"; // Nedbank test ACBVAL
//var serverName2 = "http://suglifeabiqa01:89"; // NedBank test Suburb
var serverName = "Http://2008-4.quantumsolutions.co.za:91"; // Quantum Cloud Test acbval Autocompletes
var serverName2 = "http://2008-4.quantumsolutions.co.za:92";// Quantum Cloud Test Suburb AutoCompletes 

$(document).ready(function() {

/*
#ID  Type
f22  Claimant Title

f46  Claimant Suburb
f49  Claimant City
f52 Claimant Code

f77 Deceased Title

f92 DeceasedOccupation

f101 DeceasedSuburb
f104 DeceasedCity
f108 DeceasedCode

f178 Hospital Suburb
f180 Hospital City
f182 Hospital Code

f144 Beneficiary 1 Bank 
f145 Beneficiary 1 BranchName
f155 Beneficiary 1 BranchCode
f156 Beneficiary 1 BankCode
*/
	// the field id is generated by Electric paper and should be changed to the apporiate value 
	// if the form is changed (fields added or deleted) this id could also change
	// the best way to locate the id is to right click on the field in firefox and select "Inspect Element"
//Claimant Title
$("#f22").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Title"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		// no select function
		});	
		
//Claimant Address
$("#f49").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f46").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f49').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f52').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f52").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f49').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f46').value = $.trim(pieces[pieces.length-1]); }		
		});
		
//Deceased Address
$("#f104").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f101").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f104').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f108').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f108").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f104').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f101').value = $.trim(pieces[pieces.length-1]); }		
		});
		
//Deceased Title
$("#f77").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Title"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		// no select function
		});	
		
//Deceased Occupation
$("#f92").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Occupation"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		// no select function
		});

//Hospital Address Details
$("#f180").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"City"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		// no select function
		});				
		
$("#f178").autocomplete({ // display PostalCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Suburb","parent":"' + document.getElementById('f180').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f182').value = $.trim(pieces[pieces.length-1]); }		
		});
		
$("#f182").autocomplete({ 
		source: function( request, response ) {
			$.ajax({
				url: serverName2+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"Code","parent":"' + document.getElementById('f180').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f178').value = $.trim(pieces[pieces.length-1]); }		
		});	

//Payment Bank and Branch Details

$("#f156").autocomplete({ //Display BankCode in a Different field
		source: function( request, response ) {
			$.ajax({
				url: serverName+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"BankId"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f144').value = $.trim(pieces[pieces.length-1]); }
		});
		


$("#f144").autocomplete({ //Display BankCode in a Different field
		source: function( request, response ) {
			$.ajax({
				url: serverName+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"BankName"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1 
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f156').value = $.trim(pieces[pieces.length-1]); }
		});


$("#f145").autocomplete({ // display BranchCode in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"BranchName","parent":"' + document.getElementById('f144').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f155').value = $.trim(pieces[pieces.length-1]); }		
		});

$("#f155").autocomplete({ // display BrancName in a different field
		source: function( request, response ) {
			$.ajax({
				url: serverName+"/ACBServiceProxy.svc/Autolookup", type: 'POST', dataType: 'json', contentType: 'application/json; charset="utf-8"',				
				data: '{"search":"' + request.term + '","type":"BranchCode","parent":"' + document.getElementById('f144').value + '"}',
				success: function (data) { response($.map(data.AutolookupResult, function (item) { return { label: item.Key + (item.Key==item.Val?"":" | " + item.Val),value: item.Key }})); }
			});
		}, minLength: 1
		, select: function( event, ui ) { pieces = ui.item.label.split(" | "); document.getElementById('f145').value = $.trim(pieces[pieces.length-1]); }				
		});	
});
	